package com.ice.base.controller;

import com.ice.base.constant.HCNetSDK;
import com.ice.base.util.FaceEngineTool;
import com.ice.base.util.FaceOpenBfUtil;
import com.ice.base.util.RedisUtil;
import com.ice.body.hardware.model.DeviceFace;
import com.ice.body.hardware.service.DeviceFaceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 启动加载类
 */
@Component
@Slf4j
@Order(value = 1)
public class LoadController implements ApplicationRunner {

    @Autowired
    private DeviceFaceService deviceFaceService;
    @Autowired
    FaceEngineTool faceEngineTool;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private FaceOpenBfUtil faceOpenBfUtil;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        redisUtil.flushDB();

        // 初始化SDK
        HCNetSDK sdk = HCNetSDK.INSTANCE;
        if (!sdk.NET_DVR_Init()) {
            log.error("初始化人脸识别失败");
            return;
        }
        try{
            // 初始化开门布防
            faceOpenBfUtil.faceOpenBf(sdk);
            log.info("*** --- 初始化开门布防完成 --- ***");
        }catch (Exception e){
            log.error("*** --- 初始化开门布防失败 --- ***");
            e.printStackTrace();
        }

     /*   try{
            // 初始化关门布防
            faceCloseBfUtil.faceCloseBf(sdk);
            log.info("*** --- 初始化关门布防成功 --- ***");
        }catch (Exception e){
            log.error("*** --- 初始化关门布防失败 --- ***");
            e.printStackTrace();
        }*/


      /*  try{
            // rfidService.UHT_openRFID("COM3",115200);
            log.info("*** --- 初始服务器白盒扫描COM3：115200成功 --- ***");
        }catch (Exception e){
            log.error("*** --- 初始服务器白盒扫描COM3：115200失败 --- ***");
            e.printStackTrace();
        }*/

 /*       try{
            nettySever.startServer();
            log.info("*** --- 初始化socket 网口监听7001成功 --- ***");
        }catch (Exception e){
            log.error("*** --- 初始化socket 网口监听7001失败 --- ***");
            e.printStackTrace();
        }
*/
    }

    @Scheduled(cron = "0 0/30 * * * ?")
    public void syncTimeFaceHandle(){
       // System.out.println("人脸时间同步:"+ LocalDateTime.now());
        ArrayList<DeviceFace> list = deviceFaceService.selectAll();
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        list.forEach(face->{
           syncTimeForFace(face.getIp(),face.getPort(),face.getUserName(),face.getPwd());
        });
    }

    public void syncTimeForFace(String ip,String port,String user,String pwd){
        // 初始化SDK
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//        HCNetSDK.INSTANCE.NET_DVR_Init();
        HCNetSDK.NET_DVR_DEVICEINFO m_strDeviceInfo= new HCNetSDK.NET_DVR_DEVICEINFO();
        int lUserID = HCNetSDK.INSTANCE.NET_DVR_Login(ip,(short)Integer.parseInt(port),user,pwd,m_strDeviceInfo);
        HCNetSDK.NET_DVR_TIME now = new  HCNetSDK.NET_DVR_TIME();
        String time = sdf.format(new Date());
        now.dwYear=Integer.parseInt(time.substring(0,4));
        now.dwMonth=Integer.parseInt(time.substring(4,6));
        now.dwDay=Integer.parseInt(time.substring(6,8));
        now.dwHour=Integer.parseInt(time.substring(8,10));
        now.dwMinute=Integer.parseInt(time.substring(10,12));
        now.dwSecond=Integer.parseInt(time.substring(12,14));
        int l = 0xffffffff;
        now.write();
        boolean flag =HCNetSDK.INSTANCE.NET_DVR_SetDVRConfig(lUserID, HCNetSDK.INSTANCE.NET_DVR_SET_TIMECFG,l,now.getPointer(),now.size());
        if(flag){
            System.out.println("时间set同步成功");
        }else{
            //System.out.println(HCNetSDK.INSTANCE.NET_DVR_GetLastError()+"");
        }
        HCNetSDK.INSTANCE.NET_DVR_Logout(lUserID);
    }

}
